package ngames.com.sushitv.temp.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;



import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.R;
import ngames.com.sushitv.entity.Tag;
import ngames.com.sushitv.view.DynamicTagFlowLayout;

/**
 * Created by edmond on 16-8-1.
 */
public class TagAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<Tag> tagsArrayList;
    private LayoutInflater inflater;

    private static final int TYPE_FOOTER = 2;
    private static final int TYPE_ITEM = 1;
    private static final int TYPE_HEAD = 0;

    public TagAdapter(Context context, List<Tag> tagsArrayList){
        this.context = context;
        this.tagsArrayList = tagsArrayList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("TagAdapter","1");
        RecyclerView.ViewHolder holder = null;
        if(viewType==TYPE_FOOTER){
            View view = inflater.inflate(R.layout.footer_tag,parent,false);
            holder = new FooterHolder(view);
        }else if (viewType==TYPE_ITEM){
            View view = inflater.inflate(R.layout.item_tag,parent,false);
            holder = new ItemHolder(view);
        }else if(viewType==TYPE_HEAD){
            View view = inflater.inflate(R.layout.head_tag,parent,false);
            holder = new HeadHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position%2==0){
            holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.double_bg));
        }else{
            holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.single_bg));
        }
    }

    @Override
    public int getItemCount() {
        return tagsArrayList.size()+2;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==getItemCount()-1){
            return TYPE_FOOTER;
        }else if(position==0){
            return TYPE_HEAD;
        }else{
            return TYPE_ITEM;
        }
    }

    class HeadHolder extends RecyclerView.ViewHolder{
        DynamicTagFlowLayout dynamicTagFlowLayout;
        TextView textView;

        List<String> strings = new ArrayList<>();
        boolean isOpen = false;
        public HeadHolder(View itemView) {
            super(itemView);
            dynamicTagFlowLayout = (DynamicTagFlowLayout) itemView.findViewById(R.id.dynamicTag);
            textView = (TextView) itemView.findViewById(R.id.press);

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    strings.add("1");
                    strings.add("2");
                    strings.add("3");
                    strings.add("4");
                    dynamicTagFlowLayout.setTags(strings);
                }
            });

            strings.add("1");
            strings.add("2");
            strings.add("3");
            strings.add("4");
            strings.add("5");
            strings.add("6");
            strings.add("7");
            strings.add("8");
            strings.add("9");
            dynamicTagFlowLayout.setTags(strings);
        }
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        public ItemHolder(View itemView) {
            super(itemView);
        }
    }

    class FooterHolder extends RecyclerView.ViewHolder{
        public FooterHolder(View itemView) {
            super(itemView);
        }
    }

}
